package com.example.mobile2021606127cxz0329homework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMainTitle;
    private EditText edtTxtMainShow;
    private Button btn7;
    private Button btnjian;
    private Button btnclear;
    private Button btnbfh;
    private Button btn1;
    private Button btn9;
    private Button btnadd;
    private Button btn6;
    private Button btnequal;
    private Button btn0;
    private Button btnback;
    private Button btn3;
    private Button btn2;
    private Button btn4;
    private Button btndian;
    private Button btn5;
    private Button btnchu;
    private Button btnchen;
    private Button btn8;
    String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnadd.setOnClickListener(this);
        btnback.setOnClickListener(this);
        btnchu.setOnClickListener(this);
        btnclear.setOnClickListener(this);
        btnequal.setOnClickListener(this);
        btnchen.setOnClickListener(this);
        btnbfh.setOnClickListener(this);
        btnjian.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v;
        int id = btn.getId();
        switch (id) {
            case R.id.btn0:
                edtTxtMainShow.append("0");
                break;
            case R.id.btn1:
                edtTxtMainShow.append("1");
                break;
            case R.id.btn2:
                edtTxtMainShow.append("2");
                break;
            case R.id.btn3:
                edtTxtMainShow.append("3");
                break;
            case R.id.btn4:
                edtTxtMainShow.append("4");
                break;
            case R.id.btn5:
                edtTxtMainShow.append("5");
                break;
            case R.id.btn6:
                edtTxtMainShow.append("6");
                break;
            case R.id.btn7:
                edtTxtMainShow.append("7");
                break;
            case R.id.btn8:
                edtTxtMainShow.append("8");
                break;
            case R.id.btn9:
                edtTxtMainShow.append("9");
                break;
            case R.id.btnclear:
                edtTxtMainShow.setText(null);
                break;
            case R.id.btnback:
//                退格  123  ——>12
                String msg = edtTxtMainShow.getText().toString();
                if (msg.length() > 1) {
                    edtTxtMainShow.setText(msg.substring(0, msg.length() - 1));
                }
                break;
            case R.id.btnadd:
                String msga = edtTxtMainShow.getText().toString();
                Integer a = Integer.valueOf(msga);
                edtTxtMainShow.append("+");
                break;
            case R.id.btnjian:
                String b = edtTxtMainShow.getText().toString();
                Integer c = Integer.valueOf(b);
                edtTxtMainShow.append("-");
                break;
            case R.id.btnchen:
                String ds = edtTxtMainShow.getText().toString();
                Integer ab = Integer.valueOf(ds);
                edtTxtMainShow.append("*");
                break;
            case R.id.btnchu:
                String msgab = edtTxtMainShow.getText().toString();
                Integer e = Integer.valueOf(msgab);
                edtTxtMainShow.append("/");
                break;
            case R.id.btnbfh:
                String msgac = edtTxtMainShow.getText().toString();
                Integer q = Integer.valueOf(msgac);
                edtTxtMainShow.append("%");
                break;
            case R.id.btnequal:
                //获取文本值
                String text = edtTxtMainShow.getText().toString();
                Double d = 0.0;
                d = Utils.getResult(text);
                Log.d(TAG, "onClick: ");
                edtTxtMainShow.append("=" + d.toString());
//                Log.d("TAGAA", "onClick: "+);
//                b=?
//                a  ?  b
//                int result=a ?b ;

                break;
        }
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        edtTxtMainShow = (EditText) findViewById(R.id.edtTxtMainShow);
        btn7 = (Button) findViewById(R.id.btn7);
        btnjian = (Button) findViewById(R.id.btnjian);
        btnclear = (Button) findViewById(R.id.btnclear);
        btnbfh = (Button) findViewById(R.id.btnbfh);
        btn1 = (Button) findViewById(R.id.btn1);
        btn9 = (Button) findViewById(R.id.btn9);
        btnadd = (Button) findViewById(R.id.btnadd);
        btn6 = (Button) findViewById(R.id.btn6);
        btnequal = (Button) findViewById(R.id.btnequal);
        btn0 = (Button) findViewById(R.id.btn0);
        btnback = (Button) findViewById(R.id.btnback);
        btn3 = (Button) findViewById(R.id.btn3);
        btn2 = (Button) findViewById(R.id.btn2);
        btn4 = (Button) findViewById(R.id.btn4);
        btndian = (Button) findViewById(R.id.btndian);
        btn5 = (Button) findViewById(R.id.btn5);
        btnchu = (Button) findViewById(R.id.btnchu);
        btnchen = (Button) findViewById(R.id.btnchen);
        btn8 = (Button) findViewById(R.id.btn8);
    }
}